package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppAdRoll;
import com.irdstudio.apicenter.console.service.authenticate.AppAdRollService;

@Controller
@RequestMapping(value = "/appAdRoll")
public class AppAdRollAction extends MacmsBaseAction {
	@Autowired
	private AppAdRollService appAdRollService;

	// 插入跳转
	@RequestMapping(value = "jumpAddAdRoll")
	public String jumpAddDevice(HttpServletRequest request) {
		String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addAdRoll";
	}

	// 插入保存
	@RequestMapping(value = "addAdRoll")
	public void addAdRoll(@ModelAttribute("AppAdRoll") AppAdRoll appAdRoll,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appAdRollService.insertOne(appAdRoll);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateAdRoll")
	public String jumpUpdateAdRoll(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		String appId = request.getParameter("appId");
		AppAdRoll appAdRoll = appAdRollService.queryAppTradePrivate(recordKey);
		request.setAttribute("appAdRoll", appAdRoll);
		request.setAttribute("appid", appId);
		return "auth/updateAdRoll";
	}

	// 更新保存
	@RequestMapping(value = "updateAdRoll")
	public void updateAdRoll(@ModelAttribute("appAdRoll") AppAdRoll appAdRoll,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appAdRollService.update(appAdRoll);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteAdRoll")
	public void deleteDevice(
			@ModelAttribute("deleteAdRoll") AppAdRoll appAdRoll,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appAdRollService.delete(appAdRoll);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "viewAdRoll")
	public String viewAdRoll(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String appId = request.getParameter("appid");
		String recordKey = request.getParameter("recordKey");
		AppAdRoll appAdRoll = appAdRollService
				.queryAppTradePrivate(recordKey);
		request.setAttribute("appAdRoll", appAdRoll);
		request.setAttribute("appid", appId);
		return "auth/queryAdRollDetail";
	}
}
